tmap = {
    '0': ['o', 'O'],
    # '0': ['o'],
    '1': ['i', 'a', 'l'],
    # '1': ['i', 'a'],
    # '1': ['i'],
    '2': ['s', 'b'],
    '3': ['c'],
    '4': ['d'],
    '8': ['b'],
    '9': ['g'],
    '00': ['o0', '0o', 'oo', 'oO', 'Oo'],
    '11': [
        '1i', 'i1', 'ii', 'iI', 'Ii', 
        '1a', 'a1', 'aa', 
        '1l', 'l1', 'll', 'lL', 'Ll'
    ],
    'a': ['1'],
    'b': ['2'],
    'c': ['3'],
    'd': ['4'],
    'e': ['1'],
    'g': ['9'],
    'i': ['1'],
    'l': ['1'],
    'o': ['0'],
    'q': ['9'],
    'r': ['2'],
    's': ['2', '$'],
    'z': ['2']
}

def rreplace(data, old, new, count) :
    return new.join(data.rsplit(old, count))

def item_map_transfer(data, pattern) :
    result = []
    if not pattern in tmap :
        return result
    if -1 == data.find(pattern) :
        return result
    for rep in tmap[pattern] :
        result.append(data.replace(pattern, rep, 1))
    if data.find(pattern) != data.rfind(pattern) :
        for rep in tmap[pattern] :
            result.append(rreplace(data, pattern, rep, 1))
    return result

def map_transfer(data_list, pattern) :
    if not pattern in tmap :
        return result
    result = []
    for data in data_list :
       result += item_map_transfer(data, pattern)
    return result

# tflag[index]=1: 这个位置的数据是转换过的，不要再转换 nonono
# tflag=0: number -> alpha
# tflag=1: alpha -> number
# dflag[index]=0: data[index] is alpha
# dflag[index]=1: data[index] is number
def item_recur_transfer(number, tflag, dflag, data, index, result, result_list) :
    if not number in tmap :
        return
    if index >= len(data) :
        result_list.append(result)
        return 
    if data[index] != number or tflag == dflag[index] :
        item_recur_transfer(number, tflag, dflag, data, index + 1, result + data[index], result_list)
    else :
        item_recur_transfer(number, tflag, dflag, data, index + 1, result + number, result_list)
        for alpha in tmap[number] :
            item_recur_transfer(number, tflag, dflag, data, index + 1, result + alpha, result_list)
    

# 对一个列表中的所有项进行字母-数字映射, 返回一个列表
def recur_transfer(number, tflag, data_list) :
    result = []
    for data in data_list :
        dflag = [data[i] >= '0' and data[i] <= '9' for i in range(len(data))]
        item_recur_transfer(number, tflag, dflag, data, 0, '', result)
    return result

if __name__ == "__main__" :

    print(item_map_transfer("20001011", "00"))
    print(item_map_transfer("20001011", "11"))

    print(map_transfer(["20001011"], "00"))
    print(map_transfer(["20001011"], "11"))
